<template>
  <div class="login">
    <el-form
      ref="registerForm"
      :model="registerForm"
      :rules="registerRules"
      class="login-form"
      status-icon
    >
      <div class="login-header">
        <h3 class="title">{{ title }}</h3>
        <p class="subtitle">欢迎注册新账户</p>
      </div>

      <el-form-item prop="username">
        <el-input
          v-model="registerForm.username"
          type="text"
          placeholder="请输入账号"
          clearable
          class="custom-input"
        >
          <template #prefix>
            <svg-icon icon-class="user" class="input-icon" />
          </template>
        </el-input>
      </el-form-item>

      <el-form-item prop="password">
        <el-input
          v-model="registerForm.password"
          type="password"
          placeholder="请输入密码"
          show-password
          clearable
          class="custom-input"
        >
          <template #prefix>
            <svg-icon icon-class="password" class="input-icon" />
          </template>
        </el-input>
      </el-form-item>

      <el-form-item prop="confirmPassword">
        <el-input
          v-model="registerForm.confirmPassword"
          type="password"
          placeholder="请再次输入密码"
          show-password
          clearable
          class="custom-input"
        >
          <template #prefix>
            <svg-icon icon-class="password" class="input-icon" />
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="registerForm.code"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter.native="handleRegister"
        >
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="register-code">
          <img :src="codeUrl" @click="getCode" class="register-code-img"/>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button
          :loading="loading"
          type="primary"
          class="login-btn"
          @click.native.prevent="handleRegister"
        >
          <span v-if="!loading">注 册</span>
          <span v-else>注 册 中...</span>
        </el-button>
      </el-form-item>

      <div class="register-link">
        <router-link class="link-type" :to="'/login'">使用已有账户登录</router-link>
      </div>
    </el-form>

    <div class="el-login-footer">
      <span></span>
    </div>
  </div>
</template>

<script>
import { getCodeImg, register } from "@/api/login"

export default {
  name: "Register",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error("两次输入的密码不一致"))
      } else {
        callback()
      }
    }
    return {
      title: process.env.VUE_APP_TITLE,
      codeUrl: "",
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
        code: "",
        uuid: ""
      },
      registerRules: {
        username: [
          { required: true, message: "请输入您的账号", trigger: "blur" },
          { min: 2, max: 20, message: '用户账号长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],
        password: [
          { required: true, message: "请输入您的密码", trigger: "blur" },
          { min: 5, max: 20, message: "密码长度应为 5 到 20 位", trigger: "blur" },
          { pattern: /^[^<>"'|\\]+$/, message: "不能包含非法字符", trigger: "blur" }
        ],
        confirmPassword: [
          { required: true, message: "请再次输入密码", trigger: "blur" },
          { validator: equalToPassword, trigger: "blur" }
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "change" }
        ]
      },
      loading: false,
      captchaEnabled: false
    }
  },
  created() {
    this.getCode()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled !== false
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.registerForm.uuid = res.uuid
        }
      })
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true
          register(this.registerForm).then(() => {
            this.$alert(
              `<font color='red'>恭喜你，您的账号 ${this.registerForm.username} 注册成功！</font>`,
              '系统提示',
              { dangerouslyUseHTMLString: true, type: 'success' }
            ).then(() => {
              this.$router.push("/login")
            }).catch(() => {})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

.login {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5d0fe, #c084fc);
  padding: 20px;

  &::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: url("../assets/images/img6.png");
    background-size: cover;
    opacity: 1;
    z-index: 0;
  }

  .login-form {
    position: relative; // 确保表单在伪元素上方
    z-index: 1;
    width: 100%;
    max-width: 420px;
    padding: 40px 30px;
    background: transparent; /* 改为完全透明 */
    border-radius: 20px;


    .login-header {
      text-align: center;
      margin-bottom: 30px;


      .title {
        margin-bottom: 8px;
        font-size: 28px;
        color: #a855f7;
        font-weight: bold;
      }

      .subtitle {
        font-size: 14px;
        color: #8818cf;
      }
    }

    .el-form-item {
      margin-bottom: 20px;
    }

    .custom-input {
      flex: 1;
      :deep(.el-input__wrapper) {
        padding: 0 16px;
        border-radius: 14px;
        background-color: rgba(255, 255, 255, 0.7);
        border: 1px solid rgba(233, 213, 255, 0.6); // ✅ 紫色边框
        box-shadow: 0 2px 8px rgba(192, 132, 252, 0.2); // ✅ 柔和阴影
        backdrop-filter: blur(5px); // ✅ 毛玻璃效果
        transition: all 0.3s ease;

        &.is-focus {
          border-color: #a855f7; // ✅ 聚焦时高亮边框
          box-shadow: 0 0 0 2px rgba(168, 85, 247, 0.3);
        }
      }

      :deep(.el-input__inner) {
        height: 46px;
        font-size: 14px;
        color: #ffffff; // ✅ 白色字体适配深色背景
        background-color: transparent; // ✅ 透明内层背景
        caret-color: #f472b6; // ✅ 光标颜色为粉色
      }
    }


    .input-icon {
      color: #c084fc;
      font-size: 18px;
    }

    .code-container {
      display: flex;
      align-items: center;
      gap: 12px;

      .code-image-wrapper {
        flex-shrink: 0;
        width: 120px;
        height: 46px;
        border-radius: 12px;
        overflow: hidden;
        cursor: pointer;
        box-shadow: 0 2px 8px rgba(192, 132, 252, 0.2);
        transition: all 0.3s;

        &:hover {
          transform: scale(1.02);
        }

        .login-code-img {
          width: 100%;
          height: 100%;
          object-fit: cover;
        }
      }
    }

    .remember-me {
      :deep(.el-checkbox__label) {
        color: #7e6bc1;
      }
    }

    .login-btn {
      width: 100%;
      height: 46px;
      font-size: 16px;
      font-weight: bold;
      border-radius: 14px;
      background: linear-gradient(135deg, #a855f7, #f9a8d4);
      color: white;
      border: none;
      box-shadow: 0 4px 14px rgba(168, 85, 247, 0.4);
      transition: all 0.3s;

      &:hover {
        background: linear-gradient(135deg, #9333ea, #f472b6);
        transform: translateY(-1px);
      }
    }

    .link-type {
      display: inline-block;
      margin-top: 8px;
      font-size: 14px;
      color: #a855f7;
      text-align: center;
      width: 100%;
      transition: 0.2s;

      &:hover {
        text-decoration: underline;
      }
    }
  }

  .el-login-footer {
    position: fixed;
    bottom: 20px;
    width: 100%;
    text-align: center;
    color: #c084fc;
    font-size: 12px;
  }
}
</style>
